﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace DxSnap
{
    public partial class Form1 : Form
    {
        int picDelay = 0;	//定格3秒
        int snapCounter = 0;

        string snapDir = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = this.Owner.Width;
            this.Height = this.Owner.Height;
            this.Left = this.Owner.Left;
            this.Top = this.Owner.Top;

            //snap dir
            snapDir = Path.Combine(Application.StartupPath, "snap");
            if (!System.IO.Directory.Exists(snapDir))
                Directory.CreateDirectory(snapDir);

            this.timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            snapCounter++;

            //45 seconds snap once
            if (snapCounter >= 10)
            {
                //snap screen
                Bitmap screenBitmap = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
                Graphics g = Graphics.FromImage(screenBitmap);
                Point offset = this.PointToScreen(new Point(0, 0));
                g.CopyFromScreen(offset.X, offset.Y, 0, 0, this.ClientSize);
                g.Dispose();

                //save screen snap
                string snapFile = Path.Combine(snapDir, DateTime.Now.ToString("yyyyMMddHHmmss"));
                snapFile += "." + DeviceSettings.Default.SnapExt;
                screenBitmap.Save(snapFile);
                screenBitmap.Dispose();

                //popup
                PopupSnap popup = new PopupSnap(snapFile);
                popup.StartPosition = FormStartPosition.Manual;
                popup.Location = this.Location;
                popup.Width = this.Width;
                popup.Height = this.Height;                
                popup.Show(this);

                //reset counter
                snapCounter = 0;
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                Application.Exit();
            }
            catch { }
        }

        private void Form1_Move(object sender, EventArgs e)
        {
            this.Owner.Left = this.Left;
            this.Owner.Top = this.Top;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.O)
                {
                    DeviceOption opt = new DeviceOption();
                    opt.Show(this);
                }
            }
        }
    }
}
